#include "HtmlDocument.h"

#include "stringdef.h"
#include "RPointerStack.h"
#include <e32base.h>
#include "element.h"
#include "StringUtil.h"
#include "RPointerStack.h"

using namespace Util;
namespace DOM
{
HtmlDocument::HtmlDocument()
{
	
}

HtmlDocument::~HtmlDocument()
{
}

void HtmlDocument::LoadStrL(const TDesC& aDes)
{
	TInt pos1 = -1;
	TInt pos2 = -1;
	
	_LIT(KTextPre,"<");
	_LIT(KTextTail,">");
	
	RPointerStack<Node> nodeStack;
	//StringUtil strUtil;  //-
	TInt startPos = 0;
	while(1)
		{
		pos1 = StringUtil::Find(aDes,KTextPre,startPos);
		if(pos1 == KErrNotFound)
			break;
		
		pos1 += startPos;
		//text
		if(pos1 != startPos)
			{
			TInt nTextLen = pos1 - startPos;
			HBufC* pTextBuf = HBufC::NewL(nTextLen);
			CleanupStack::PushL(pTextBuf);
			TPtr ptrTextBuf = pTextBuf->Des();
			ptrTextBuf.Copy(aDes.Mid(startPos,nTextLen));
			//dosomething

			CleanupStack::PopAndDestroy(1);
			}
		
		startPos = pos1 + 1;
		pos2 = StringUtil::Find(aDes,KTextTail,startPos);
		if(pos2 == KErrNotFound)
			break;
		
		pos2 += startPos;
		startPos = pos2 + 1;
		
		TPtrC ptrNodeDes = aDes.Mid(pos1+1,pos2-pos1-1);
		Node* pNode = Node::CreateNodeL(ptrNodeDes);
		
		if(pNode->NodeType() == Node::NTElement)
			{
			if(pNode->TagType() == Node::TTBegin)
				{
				Node* pParentNode = nodeStack.Top();
				if(pParentNode)
					{
					pParentNode->AppendChild(pNode);
					nodeStack.Push(pNode);
					}
				else
					{
					Node* pBodyNode = BodyNode();
					if(!pBodyNode) 
						{
						_LIT(KTextCreateBody,"body");
						pBodyNode = Node::CreateNodeL(KTextCreateBody);
						AppendChild(pBodyNode);
						}
					
					if(pBodyNode)
						{
						pBodyNode->AppendChild(pNode);
						nodeStack.Push(pNode);
						}
					}
				}
			else if(pNode->TagType() == Node::TTEnd)
				{
				Node* pParentNode = nodeStack.Top();
				if(pParentNode)
					{
					nodeStack.Pop();
					}
				
				delete pNode;
				pNode = NULL;
				}
			else
				{
				Node* pParentNode = nodeStack.Pop();
				if(pParentNode)
					{
					pParentNode->AppendChild(pNode);
					}
				else
					{
					Node* pBodyNode = BodyNode();
					if(!pBodyNode) 
						{
						_LIT(KTextCreateBody,"body");
						pBodyNode = Node::CreateNodeL(KTextCreateBody);
						AppendChild(pBodyNode);
						}
					
					if(pBodyNode)
						{
						pBodyNode->AppendChild(pNode);
						}
					}
				}
			}
		else if(pNode->NodeType() == Node::NTStyle)
			{
			
			}

		}
	
}

Node* HtmlDocument::BodyNode()
{
	for(Node* pNode = FirstChild(); pNode ; pNode = pNode->NextSibling())
		{
		if(pNode->NodeType() == Node::NTElement)
			{
			TBuf<10> tag;
			((Element*)pNode)->TagName(tag);
			
			if(tag == KDOMStrBody)
				return pNode;
			}
		
		}
	
	return NULL;
}


//TBool HtmlDocument::HasBodyNode() const
//{
//	for(Node* pNode = FirstChild(); pNode ; pNode = pNode->NextSibling())
//		{
//		if(pNode->NodeType() == Node::NTElement)
//			{
//			TBuf<10> tag;
//			((Element*)pNode)->TagName(tag);
//			
//			if(tag == KDOMStrBody)
//				return ETrue;
//			}
//		
//		}
//
//	return EFalse;
//}

}
